package com.zijidelu.luframework.infrastructure.persistence.repository.generic.domain;

import com.zijidelu.luframework.config.properties.LuFrameworkProperties;
import com.zijidelu.luframework.constant.DomainConstant;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 通用领域键计算实现类。
 *
 * @author ZIJIDELU
 * @datetime 2024/11/25 18:09
 */
@Component
@RequiredArgsConstructor
public class GenericDomainKeyCalculatorImpl implements GenericDomainKeyCalculator {
    @Setter(onMethod_ = {@Autowired})
    private LuFrameworkProperties luFrameworkProperties;

    /**
     * {@inheritDoc}
     */
    @Override
    public String calcGenericDomainKey(String... params) {
        return calcGenericDomainKey(Arrays.asList(params));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String calcGenericDomainKey(List<String> paramList) {
        // 初始化 Key 片段列表
        List<String> segments = new ArrayList<>();

        // 系统名前缀
        String appName = luFrameworkProperties.getApplicationName();
        segments.add(appName);

        // 通用领域标识
        segments.add(DomainConstant.GENERIC_DOMAIN_PREFIX);

        // 拼接动态参数部分
        if (paramList != null && !paramList.isEmpty()) {
            segments.addAll(paramList);
        }

        // 使用统一分隔符连接
        return String.join(DomainConstant.DOMAIN_SPLIT, segments);
    }
}
